package com.xy.manage.action;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.manage.model.Tflash;
import com.xy.manage.service.TflashService;



@Controller
public class TflashAction{
	private static final Logger log = LoggerFactory.getLogger(TflashAction.class);
	public TflashAction() {
	}
 
 
 	/**
	 * 视频快照新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tflashAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tflash", new Tflash());
        setDict(model);
		return "tflashEdit";
	}

	/**
	 * 视频快照新增保存
	 * 
	 * @param tflash
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tflashSave.do")
	public String save(Tflash tflash, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tflash, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tflash = tflashService.save(tflash);
		return "redirect:v_tflashList.do";
	}

	/**
	 * 视频快照删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tflashDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tflashService.remove(id);
			}
		}
		return "redirect:v_tflashList.do";
	}

	/**
	 * 视频快照编辑页面
	 * 
	 * @param fid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tflashEdit.do")
	public String edit(long fid, HttpServletRequest request, ModelMap model) {
		Tflash tflash = tflashService.get(fid);
		model.addAttribute("tflash", tflash);
      model.addAttribute("actionEdit", request.getParameter("actionEdit"));
        setDict(model);
		return "tflashEdit";
	}

	/**
	 * 视频快照详情
	 * 
	 * @param fid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tflashDetail.do")
	public String detail(long fid, HttpServletRequest request, ModelMap model) {
		Tflash tflash = tflashService.get(fid);
		model.addAttribute("tflash", tflash);
        setDict(model);
		return "tflashView";
	}

	/**
	 * 视频快照查询（分页）
	 * 
	 * @param tflash
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tflashList.do")
	public String listPage(Tflash tflash, Integer pageNo,
			HttpServletRequest request, ModelMap model,Integer flag) {
		if(null!=flag){
			model.addAttribute("updateflag",flag==1?true:false);
		}
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = null;
		if(null!=tflash.getLoca()&&!"".equals(tflash.getLoca())){
			pagination = tflashService.getEntityByFuzzLoca(tflash.getLoca(), pageNo, CookieUtils.getPageSize(request));
		} else {
			pagination = tflashService.getTflashByPage(toMap(tflash), pageNo, CookieUtils.getPageSize(request));
		}
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tflashList";
	}

	/**
	 * 视频快照查询
	 * 
	 * @param tflash
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tflashList1.do")
	public String list(Tflash tflash, HttpServletRequest request, ModelMap model) {
		List<Tflash> list = tflashService.getTflash(toMap(tflash));
		model.addAttribute("list", list);
        setDict(model);
		return "tflashList";
	}
	
	@RequestMapping("o_tflashUpdatePath.do")
	public String updatePath(HttpServletRequest request, ModelMap model) {
		if(tflashService.updatePath())
			return "redirect:v_tflashList.do?flag=1";
		else
			return "redirect:v_tflashList.do?flag=0";
	}
	

 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tflash
	 * @return
	 */	private Map<String,Object> toMap(Tflash tflash){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tflash.getFid()!=null&&!tflash.getFid().equals("")){
			map.put("fid", tflash.getFid());
		}
		if(tflash.getPicpath()!=null&&!tflash.getPicpath().equals("")){
			map.put("picpath", tflash.getPicpath());
		}
		if(tflash.getLoca()!=null&&!tflash.getLoca().equals("")){
			map.put("loca", tflash.getLoca());
		}
		if(tflash.getPosition()!=null&&!tflash.getPosition().equals("")){
			map.put("position", tflash.getPosition());
		}
		if(tflash.getStatus()!=null&&!tflash.getStatus().equals("")){
			map.put("status", tflash.getStatus());
		}
		if(tflash.getUpdatetime()!=null&&!tflash.getUpdatetime().equals("")){
			map.put("updatetime", tflash.getUpdatetime());
		}
		if(tflash.getPiccode()!=null&&!tflash.getPiccode().equals("")){
			map.put("piccode", tflash.getPiccode());
		}      
		if(tflash.getInnerpath()!=null&&!tflash.getInnerpath().equals("")){
			map.put("innerpath", tflash.getInnerpath());
		}  
		if(tflash.getArea()!=null&&!tflash.getArea().equals("")){
			map.put("area", tflash.getArea());
		} 
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
	      model.addAttribute("statusMap", DictUtils.getDictByType(DictUtils.TFLASH_STATUS));	
	      model.addAttribute("areaMap", DictUtils.getDictByType(DictUtils.TRENT_PCS));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tflash tflash, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TflashService tflashService;   
}